﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Business;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.WebApp
{
	public partial class ViewAllTicketsOfUser : System.Web.UI.Page
	{
		private Collection<Ticket> _allTickets ;
		private const int ticketsByPage = 10;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!User.Identity.IsAuthenticated)
				Response.Redirect("Login.aspx");
			_allTickets = TicketDataAccess.GetAllTicketsByBuyerId(UserDataAccess.GetUserByEmail(User.Identity.Name).Id);
			if(_allTickets==null)
				MultiViewShowTickets.ActiveViewIndex = 1;
			else
			{
				if (_allTickets.Count < 0)
					MultiViewShowTickets.ActiveViewIndex = 1;
				else
				{
					if (!IsPostBack)
					{
						Session["numPage"] = 1;
						ChangeTicketsPage();
					}
					MultiViewShowTickets.ActiveViewIndex = 0;
				}
			}
		}

		private void ChangeTicketsPage()
		{
			PrevButton.Enabled = true;
			NextButton.Enabled = true;
			int max = _allTickets.Count;
			int numPage = int.Parse(Session["numPage"].ToString());
			if (numPage == 1)
				PrevButton.Enabled = false;
			if ((max - ((numPage - 1) * ticketsByPage)) < ticketsByPage)
				NextButton.Enabled = false;
			ShowContent.InnerText = "Showing " + (((numPage - 1) * ticketsByPage) + 1) + "-" + ((((numPage) * ticketsByPage) > max) ? max : ((numPage) * ticketsByPage)) + " of " + max;
			ShowContent2.InnerText = ShowContent.InnerText;
			if (_allTickets.Count == 0)
			{
				MultiViewShowTickets.ActiveViewIndex = 1;
			}
			else
			{
				Collection<Ticket> actual = new Collection<Ticket>();
				int min = (numPage - 1) * ticketsByPage;
				int maxElem = (numPage * ticketsByPage) - 1;
				for (int i = 0; i <= maxElem && i < _allTickets.Count; i++)
				{
					if (i >= min && i <= maxElem)
					{
						actual.Add(_allTickets[i]);
					}
				}
				dataListTickets.DataSource = actual.Reverse();
				dataListTickets.DataBind();
				MultiViewShowTickets.ActiveViewIndex = 0;
			}
		}

		private bool bSwitch = false;
		string color1 = "#ffffcc";
		string color2 = "#ccff99";

		public string TRJavaScript(Control con)
		{
			string tmp;
			DataListItem dli = con as DataListItem;
			Button btn = dli.FindControl("HiddenButton") as Button;
			string _js = "bgcolor={0} onMouseover='rowcolor=this" +
						 ".style.backgroundColor;this.style.backgroundColor" +
						 "=\"#3670C7\"; this.style.color=\"white\"; this.style.cursor = \"hand\"' " +
						 "onMouseout='this.style.backgroundColor=\"white\"; this.style.color=\"black\";' " +
						 " onclick='document.getElementById(\"{1}\").click();' ";
			tmp = bSwitch ? string.Format(_js, color1, btn.ClientID) :
						   string.Format(_js, color2, btn.ClientID);
			bSwitch = !bSwitch;
			return tmp;
		}

		protected void DL_ItemCommand(object source, DataListCommandEventArgs e)
		{
			long ticketid = long.Parse(e.CommandArgument.ToString(), CultureInfo.InvariantCulture);
			Response.Redirect("ViewTicket.aspx?ticketId=" + ticketid);
		}

		protected void PrevButton_Click(object sender, EventArgs e)
		{
			int pageNum = int.Parse(Session["numPage"].ToString());
			pageNum--;
			Session["numPage"] = pageNum;
			ChangeTicketsPage();
		}

		protected void NextButton_Click(object sender, EventArgs e)
		{
			int pageNum = int.Parse(Session["numPage"].ToString());
			pageNum++;
			Session["numPage"] = pageNum;
			ChangeTicketsPage();
		}
	}
}